import dayjs from 'dayjs';

/**
 * 格式化流量显示（自动选择 GB/MB）
 * @param {number} bytes - 字节数
 * @returns {string} 格式化后的流量字符串
 */
export const formatTraffic = (bytes) => {
  const MB = 1024;
  const GB = MB;

  if (bytes >= GB) {
    return {
      traffic: Math.floor(Number((bytes / GB).toFixed(1))),
      unit: 'GB',
    };
  } else {
    return {
      traffic: Math.floor(Number(Number(bytes).toFixed(1))),
      unit: 'MB',
    };
  }
};

/**
 * 进阶版本（支持更多单位）
 * @param {number} bytes - 字节数
 * @returns {string} 格式化后的流量字符串
 */
export const formatTrafficAdvanced = (bytes) => {
  const units = ['B', 'KB', 'MB', 'GB', 'TB'];
  let unitIndex = 0;

  while (bytes >= 1024 && unitIndex < units.length - 1) {
    bytes /= 1024;
    unitIndex++;
  }

  return {
    traffic: Math.floor(bytes.toFixed(1)),
    unit: units[unitIndex],
  };
};

/**
 * 计算IP类型和天数对应的价格
 * @param {number} ipType IP类型
 * @param {number} days 天数
 * @returns {string} 价格字符串，无效输入返回空字符串
 */
export const calculatePrice = (ipType, days, quantity) => {
  const priceMap = {
    2: { 7: '1.20', 30: '3', 60: '5.70', 90: '8.10', 360: '28.80' },
    3: { 7: '2', 30: '5', 60: '9.50', 90: '13.50', 360: '48' },
  };
  return Math.abs(Number((priceMap?.[ipType]?.[days] * quantity)?.toFixed(2)));
};
